home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1998 October / Macworld (1998-10).dmg / Shareware World / Info / For Developers / MacZoop 1.8.4 / More Classes / Streaming Classes / ZHandleStream.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-04  |  2.3 KB  |  118 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************************
  2. *
  3. *
  4. *            MacZoop - "the framework for the rest of us"         
  5. *
  6. *
  7. *
  8. *            ZHandleStream.cpp        -- a stream using Handle storage
  9. *
  10. *
  11. *
  12. *
  13. *
  14. *            © 1998, Graham Cox
  15. *
  16. *
  17. *
  18. *
  19. *************************************************************************************************/
  20.  
  21.  
  22. #include    "ZHandleStream.h"
  23. #include    "ZErrors.h"
  24.  
  25.  
  26. /*-------------------------------***  CONSTRUCTOR  ***---------------------------------*/
  27.  
  28. ZHandleStream::ZHandleStream( Handle aHandle, Boolean becomeOwner )
  29.     : ZStream()
  30. {
  31.     FailNILParam( aHandle );
  32.     
  33.     h = aHandle;
  34.     hState = HGetState( h );
  35.  
  36.     ownsHandle = becomeOwner;
  37. }
  38.  
  39.  
  40. /*-------------------------------***  CONSTRUCTOR  ***---------------------------------*/
  41.  
  42. ZHandleStream::ZHandleStream()
  43.     : ZStream()
  44. {
  45.     FailNIL( h = NewHandle( 0 ));
  46.     hState = HGetState( h );
  47.     
  48.     ownsHandle = TRUE;
  49. }
  50.  
  51.  
  52. /*--------------------------------***  DESTRUCTOR  ***---------------------------------*/
  53.  
  54. ZHandleStream::~ZHandleStream()
  55. {
  56.     HSetState( h, hState );
  57.     
  58.     if ( ownsHandle )
  59.         DisposeHandle( h );
  60. }
  61.  
  62.  
  63. /*-----------------------------------***  PUTTO  ***------------------------------------*/
  64. /*
  65. write data to stream storage (handle)
  66. ----------------------------------------------------------------------------------------*/
  67.  
  68. void    ZHandleStream::PutTo( void* data, long dataLen )
  69. {
  70.     long    hLen = GetHandleSize( h );
  71.     
  72.     if (( mark + dataLen ) > hLen )
  73.     {
  74.         // need to extend handle for this data
  75.         
  76.         SetHandleSize( h, mark + dataLen );
  77.         FailMemError();
  78.     }
  79.     
  80.     HLock( h );
  81.     
  82.     Ptr     p = *h + mark;
  83.     
  84.     BlockMoveData((Ptr) data, p, dataLen );
  85.     HSetState( h, hState );
  86.     
  87.     mark += dataLen;
  88. }
  89.  
  90.  
  91. /*----------------------------------***  GETFROM  ***-----------------------------------*/
  92. /*
  93. read data from stream storage (handle)
  94. ----------------------------------------------------------------------------------------*/
  95.  
  96. void    ZHandleStream::GetFrom( void* data, long* dataLen )
  97. {
  98.     long    hLen = GetHandleSize( h );
  99.     
  100.     // see if there's enough data in the handle to satisfy the request:
  101.     
  102.     if (( mark + *dataLen ) > hLen )
  103.     {
  104.         *dataLen = hLen - mark;
  105.         mark = hLen;
  106.         FailOSErr( kNoMoreDataInStreamErr );
  107.     }
  108.         
  109.     HLock( h );
  110.  
  111.     Ptr        p = *h + mark;
  112.     
  113.     BlockMoveData( p, (Ptr) data, *dataLen );
  114.     HSetState( h, hState );
  115.     
  116.     mark += *dataLen;
  117. }    
  118.